package com.yunjing.mvparmsdemo.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseApplication;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.yunjing.mvparmsdemo.di.component.DaggerNewComponent;
import com.yunjing.mvparmsdemo.mvp.api.Api;
import com.yunjing.mvparmsdemo.mvp.api.entity.NewListEntity;
import com.yunjing.mvparmsdemo.mvp.contract.NewContract;
import com.yunjing.mvparmsdemo.mvp.presenter.NewPresenter;
import com.yunjing.mvparmsdemo.R;
import com.yunjing.mvparmsdemo.mvp.ui.adapter.NewListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 01/30/2023 10:04
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class NewActivity extends BaseActivity<NewPresenter> implements NewContract.View {

    @BindView(R.id.srl_new_list)
    SmartRefreshLayout mSrlNewList;
    @BindView(R.id.rsv_new_list)
    RecyclerView mRsvNewList;
    private NewListAdapter mNewListAdapter;
    private int nowPage = 1;
    private List<NewListEntity.ResultBean.DataBean> totalData = new ArrayList<>();

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerNewComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_new; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        mRsvNewList.setLayoutManager(linearLayoutManager);
        mNewListAdapter = new NewListAdapter(this);
        mNewListAdapter.setOnItemClickListener(new NewListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, NewListEntity.ResultBean.DataBean dataBean) {
                String url = dataBean.getUrl();
                if (TextUtils.isEmpty(url)) {
                    ArmsUtils.makeText(BaseApplication.getContext(), "该新闻不存在详情内容数据");
                } else {
                    Intent intent = new Intent(NewActivity.this, NewDetailsActivity.class);
                    intent.putExtra("web_url", url);
                    startActivity(intent);
                }

            }
        });
        mRsvNewList.setAdapter(mNewListAdapter);
        mSrlNewList.autoRefresh();
        //SmartRefreshLayout的刷新加载监听
        mSrlNewList.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                nowPage++;
                if (mPresenter != null) {
                    mPresenter.onGetNewList(nowPage);
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (mPresenter != null) {
                    mPresenter.onGetNewList(1);
                }
            }
        });

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void onGetNewListResultView(NewListEntity entities) {

        Log.d(TAG, "onGetNewListResultView: " + new Gson().toJson(entities));

        if (Api.REQUEST_SUCCESS.equals(entities.getReason())
                || Api.REQUEST_SUCCESS_SPECIAL.equals(entities.getReason())) {
            //刷新的时候，清除列表全部数据，只加载第一页
            if (mSrlNewList.isRefreshing()) {
                totalData.clear();
            }
            totalData.addAll(entities.getResult().getData());
            mNewListAdapter.setData(totalData);
            mNewListAdapter.notifyDataSetChanged();
        } else {
            Toast.makeText(this, entities.getReason(), Toast.LENGTH_SHORT).show();
        }

        if (mSrlNewList.isRefreshing()) {
            mSrlNewList.finishRefresh();
        }

        if (mSrlNewList.isLoading()) {
            mSrlNewList.finishLoadMore();
        }

    }

}
